﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using System;
using System.IO;

namespace LS.Editor.DataTableTools
{
    public sealed partial class DataTableProcessor
    {
        private sealed class UInt32Processor : GenericDataProcessor<uint>
        {
            public override bool IsSystem
            {
                get
                {
                    return true;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "uint";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "uint",
                    "uint32",
                    "system.uint32"
                };
            }

            public override uint Parse(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return uint.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                binaryWriter.Write7BitEncodedUInt32(Parse(value));
            }
        }
        private sealed class UInt32ArrayProcessor : GenericDataArrayProcessor<uint>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "uint[]";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "uint[]",
                    "uint32[]",
                    "system.uint32[]"
                };
            }

            public override uint ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return uint.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    binaryWriter.Write(array[i]);
                }
            }
        }
        private sealed class UInt32ListProcessor : GenericDataListProcessor<uint>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "List<uint>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "list<uint>",
                    "list<uint32>",
                    "list<system.uint32>",
                    "system.collections.generic.list<uint>",
                    "system.collections.generic.list<uint32>",
                    "system.collections.generic.list<system.uint32>",
                };
            }

            public override uint ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return uint.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Count);
                for (int i = 0; i < array.Count; i++)
                {
                    binaryWriter.Write(array[i]);
                }
            }
        }
        private sealed class UInt32DictionaryProcessor : GenericDataDictionaryProcessor<uint>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Dictionary<string,uint>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "dictionary<string,uint>",
                    "dictionary<string,uint32>",
                    "dictionary<string,system.uint32>",
                    "dictionary<system.string,uint>",
                    "dictionary<system.string,uint32>",
                    "dictionary<system.string,system.uint32>",
                    "system.collections.generic.dictionary<string,uint>",
                    "system.collections.generic.dictionary<string,uint32>",
                    "system.collections.generic.dictionary<string,system.uint32",
                    "system.collections.generic.dictionary<system.string,uint>",
                    "system.collections.generic.dictionary<system.string,uint32>",
                    "system.collections.generic.dictionary<system.string,system.uint32>",
                };
            }

            public override uint ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return uint.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var dic = Parse(value);
                binaryWriter.Write(dic.Count);
                foreach (var item in dic)
                {
                    binaryWriter.Write(item.Key);
                    binaryWriter.Write(item.Value);
                }
            }
        }
    }
}
